package main

import (
	"github.com/cihub/seelog"
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
	"os"
	"runtime"
	"shop/admin/admin_api"
	"shop/api"
	"shop/config"
	"shop/midware"
	"shop/service"
	"shop/utils/convertutil"
	"shop/utils/result"
	"time"
)

var startTime = convertutil.FormatStandardDate(time.Now())

const serverPort = "8888"

func main() {
	config.InitSeelog()      // 加载seelog日志配置
	service.StartTimerTask() // 开启定时任务
	// gin框架日志配置
	file, _ := os.Create("output/gin.log")
	gin.DefaultWriter = io.MultiWriter(file, os.Stdout)

	engine := gin.Default()
	engine.Use(midware.Recover)
	engine.Use(midware.Cors)
	engine.Use(midware.IpLimiter(runtime.NumCPU()*10, runtime.NumCPU()*100)) // 对于所有ip，限流所有接口
	setRouters(engine)
	setAdminRouters(engine)
	seelog.Infof("当前是否是生产环境: %v", config.IsProdEnv())
	engine.Run(":" + serverPort)
}

func setRouters(engine *gin.Engine) {
	router := engine.Group("/shop-api")
	// debug
	{
		router.GET("/ping", midware.ApiLimiter(5, 10), func(ctx *gin.Context) {
			seelog.Info("pong")
			ctx.JSON(http.StatusOK, result.Ok("启动时间: "+startTime).SetMsg("pong"))
		})
	}

	// auth
	{
		router.GET("/auth/phoneVerifyCode", midware.ApiLimiter(1, 10), api.SendPhoneVerifyCode)
		router.POST("/auth/login/weixin", api.LoginByWeixin)
		router.GET("/auth/login/getToken", midware.IpLimiter(2, 10), api.GetTokenByState)
		router.GET("/auth/login/weixinH5Auth/:shopId", api.WeixinH5Auth)
		router.GET("/tencentAuth/:shopId", api.TencentAuth)
		router.POST("/auth/login/phone", api.LoginByPhoneAndCode)
	}

	// user
	{
		router.DELETE("/user/logout", midware.Auth(), api.Logout)
		router.GET("/user/info", midware.Auth(), api.GetUserInfo)
		router.PUT("/user/info", midware.Auth(), api.UpdateUser)
	}

	// goods
	{
		router.GET("/goods/goodsMenu/list", api.GetGoodsMenuList)
	}

	// orderInfo
	{
		router.GET("/order/list", midware.Auth(), api.GetUserOrderList)
		router.GET("/order/detail/:orderNo", midware.Auth(), api.GetOrderDetail)
		router.GET("/order/status/:orderNo", midware.IpLimiter(3, 10), api.GetOrderStatus)
		router.POST("/order", midware.Auth(), api.CreateOrder)
		// 初步确认支付完成
		router.PUT("/order/confirmPay/:orderNo", midware.Auth(), api.ConfirmFinishPay)
		router.DELETE("/order/cancel/:orderNo", midware.Auth(), api.CancelOrder)
		router.PUT("/order/confirmReceive/:orderNo", midware.Auth(), api.ConfirmReceive)
	}

	// pay
	{
		// 订单支付
		router.POST("/pay/orderPay/:orderNo", midware.Auth(), api.OrderPay)
		// 支付回调
		router.POST("/pay/weixin/notify", api.WeixinPayNotify)
	}

	// shop
	{
		router.GET("/shop/info", api.GetShopInfo)
	}
}

// admin 后台管理接口
func setAdminRouters(engine *gin.Engine) {
	router := engine.Group("/shop-admin-api")

	// 认证与授权
	{
		router.GET("/auth/phoneVerifyCode", midware.ApiLimiter(1, 1), admin_api.SendPhoneVerifyCode)
		router.POST("/auth/login", admin_api.Login)
		router.POST("/auth/loginByPhone", admin_api.LoginByPhone)
	}

	// 个人中心
	{
		router.GET("/me/userInfo", midware.AdminAuth(), admin_api.GetUserInfo)
		router.PUT("/me/updatePassword", midware.AdminAuth(), admin_api.UpdatePassword)
		router.POST("/me/uploadAvatar", midware.AdminAuth(), admin_api.UploadUserAvatar) // 更新个人头像
		router.DELETE("/me/logout", midware.AdminAuth(), admin_api.Logout)
	}

	// 用户管理
	{
		router.GET("/user/list", midware.AdminAuth("user:list"), admin_api.GetUserList)
		router.PUT("/user/status/:userId/:newStatus", midware.AdminAuth("user:update"), admin_api.UpdateUserStatus) // 启用，冻结，禁用用户账号
	}

	// 商品管理
	{
		router.GET("/goods/list", midware.AdminAuth("goods:list"), admin_api.GetGoodsList)
		router.POST("/goods", midware.AdminAuth("goods:add"), admin_api.AddGoods)
		router.PUT("/goods/:id", midware.AdminAuth("goods:update"), admin_api.UpdateGoods)
		router.POST("/goods/uploadGoodsImg/:id", midware.AdminAuth("goods:update", "goods:uploadGoodsImg"), admin_api.UploadGoodsImg)
		router.DELETE("/goods/:id", midware.AdminAuth("goods:delete"), admin_api.DeleteGoods)

		// 商品种类
		router.GET("/goodsCategory/list", midware.AdminAuth("goodsCategory:list"), admin_api.GetGoodsCategoryList)
		router.POST("/goodsCategory", midware.AdminAuth("goodsCategory:add"), admin_api.AddGoodsCategory)
		router.PUT("/goodsCategory/:id", midware.AdminAuth("goodsCategory:update"), admin_api.UpdateGoodsCategory)
		router.DELETE("/goodsCategory/:id", midware.AdminAuth("goodsCategory:delete"), admin_api.DeleteGoodsCategory)

	}

	// 订单管理
	{
		router.GET("/order/list", midware.AdminAuth("order:list"), admin_api.GetOrderList)
		router.PUT("/order/status", midware.AdminAuth("order:orderStatus"), admin_api.UpdateOrderStatus)
	}

	// 店铺管理
	{
		// 店铺配置
		router.GET("/shop/config", midware.AdminAuth(), admin_api.GetShopConfig)
		router.PUT("/shop/config", midware.AdminAuth("shopConfig:update"), admin_api.UpdateShopConfig)
		// 店铺管理
		router.GET("/system/shop/list", midware.AdminAuth("shop:list"), admin_api.GetShopList)
		router.POST("/system/shop", midware.AdminAuth("shop:add"), admin_api.AddShop)
		router.PUT("/system/shop/:id", midware.AdminAuth("shop:update"), admin_api.UpdateShop)

	}

	// 管理员管理
	{
		router.GET("/system/sysUser/list", midware.AdminAuth("sysUser:list"), admin_api.GetSysUserList)
		router.POST("/system/sysUser", midware.AdminAuth("sysUser:add"), admin_api.AddSysUser)
		router.PUT("/system/sysUser/:id", midware.AdminAuth("sysUser:update"), admin_api.UpdateSysUser)
		router.DELETE("/system/sysUser/:id", midware.AdminAuth("sysUser:delete"), admin_api.DeleteSysUser)
	}

	// 角色、权限管理
	{
		router.GET("/system/role/list", midware.AdminAuth("role:list"), admin_api.GetRoleList)
		router.POST("/system/role", midware.AdminAuth("role:add"), admin_api.AddRole)
		router.PUT("/system/role/:id", midware.AdminAuth("role:update"), admin_api.UpdateRole)
		router.DELETE("/system/role/:id", midware.AdminAuth("role:delete"), admin_api.DeleteRole)
		router.GET("/system/role/permissionTree", midware.AdminAuth(), admin_api.GetPermissionTree)
	}
}
